package email;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.swing.JPanel;

public abstract class AbstractMessage {

	/**
	 * subject line of the message
	 */
	private String subject;
	/**
	 * date the message was received 
	 */
	private Date date;
	/**
	 * the id of the ReceivedMessage, note this is not the id that comes with the Javamail class Message,
	 * we must provide the our own id system
	 */
	private long id;
	
	/**
	 * Constructs the ReceivedMessage class from a given javamail Message and a given id
	 * @param m the javamail Message that this object should represent
	 * @param id the id that is associated with this message
	 */
	public AbstractMessage(Message m, long id){
		
		//call the private helper methods to set up the global variables of this message
		setId(id);
		setSubject(m);
		setDate(m);
	}
	
	public void setSubject(Message m) {
		String toSet = "";
		try{
			toSet = m.getSubject();
		}
		catch (MessagingException e){
			//do nothing will just set a blank string
		}
		subject = toSet;
	}
	
	public void setDate(Message m) {
		String toSet = "";
		try{
			//format it comes back as is Wed Nov 02 02:13:14 CDT 2011
			toSet = m.getReceivedDate().toString();
		}
		catch (MessagingException e){
			//do nothing blank date will be set
		}
		DateFormat fmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
		Date d = new Date(0);
		try {
			d = fmt.parse(toSet);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
		}
		date = d;	
	}
	
	public Date getDate() {
		return date;
	}
	
	public String getSubject() {
		return subject;
	}
	
	public long getId() {
		return id;
	}
	
	public void setId(long id) {
		this.id = id;
	}
	
	public abstract JPanel getDisplay();	
}
